package com.example.websocket.chat.controller;

import com.example.websocket.chat.bean.ChatInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.annotation.support.SendToMethodReturnValueHandler;
import org.springframework.stereotype.Controller;

/**
 * @author ZhenWuWang
 */
@Controller
public class ChatController
{
    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;

    SendToMethodReturnValueHandler handler;

    @MessageMapping("/message")
    public void handleChat(ChatInfo chatInfo)
    {
        System.out.println("========" + chatInfo.toString());
        String target = chatInfo.getTarget();
        simpMessagingTemplate.convertAndSendToUser(target,"/message",chatInfo);
    }
}
